'use strict';

const BaseController = require('./base');

class PersonController extends BaseController {
  get _service() {
    return 'person';
  }

  async create() {
    const { ctx } = this;
    const { name, No, isMy } = ctx.params();
    if (isMy) {
      await this._getService().update({ isMy: false, uid: ctx.userId });
    }
    const result = await this._getService().create({ name, No, isMy, uid: ctx.userId });
    if (result) {
      this.success();
    } else {
      this.error('创入住人失败');
    }
  }

  // 更入住人基本数据
  async update() {
    const { ctx } = this;
    const { name, No, isMy } = ctx.params();
    const { id } = ctx.params;
    if (isMy) {
      await this._getService().update({ isMy: false, uid: ctx.userId });
    }
    const result = await this._getService().update({ name, No, isMy, id });
    if (result) {
      this.success();
    } else {
      this.error('更入住人失败');
    }
  }

  async destroy() {
    await this._destory('删入住人失败');
  }

  async show() {
    await this._show('获入住人失败');
  }

  // 获取登录人入住人
  async index() {
    const { ctx } = this;
    const { pageIndex, pageSize } = ctx.params();
    const result = await this._getService().findList({ pageIndex, pageSize, uid: ctx.userId });
    if (result) {
      this.success(result.data, result.count);
    } else {
      this.error('获入住人失败');
    }
  }

  async allMy() {
    const { ctx } = this;
    const result = await this._getService().findAll({ uid: ctx.userId });
    if (result) {
      this.success(result);
    } else {
      this.error('获取入住人失败');
    }
  }
}

module.exports = PersonController;
